package com.aishua.fire.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aishua.fire.common.RequestInterfaceUtil;
import com.aishua.fire.common.constant.DictConstants;
import com.aishua.fire.entity.account.GraphicCode;
import com.aishua.fire.entity.account.Session;
import com.aishua.fire.entity.login.AppLoginRespInfo;
import com.aishua.fire.entity.login.UserLoginInfo;
import com.aishua.fire.jpa.AccountDao;
import com.aishua.fire.jpa.LoginHistoryDao;
import com.aishua.fire.jpa.entity.Account;
import com.aishua.fire.jpa.entity.LoginHistory;
import com.aishua.fire.mapper.AppOperationMapper;
import com.aishua.fire.mapper.LoginMapper;
import com.aishua.fire.service.LoginService;

@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	private LoginMapper loginMapper;
	@Autowired
	private AccountDao accountDao;
	
	@Autowired
	private LoginHistoryDao loginHistoryDao;
	
	@Autowired
	private RequestInterfaceUtil requestUtil;
	
	@Autowired
	private AppOperationMapper appMapper;

	
	/**
	 * 添加登录接口访问历史
	 * @param userLoginInfo
	 * @param loginStatus
	 */
	private void addLoginHistory(UserLoginInfo userLoginInfo,String loginStatus) {
		LoginHistory loginHistory = new LoginHistory();
		loginHistory.setClientIp(userLoginInfo.getClientIp());
		loginHistory.setClientIpCity(userLoginInfo.getClientIpCity());
		loginHistory.setClientIspName(userLoginInfo.getClientIspName());
		loginHistory.setLoginName(userLoginInfo.getUserName());
		loginHistory.setLoginPassword(userLoginInfo.getUserPwd());
		loginHistory.setLoginStatus(loginStatus);
		loginHistory.setCreateDate(new Date());
		
		loginHistoryDao.save(loginHistory);
	}
	
	/**
	 * 添加失败的登录历史记录
	 */
	@Override
	public void addFailLoginHistory(UserLoginInfo userLoginInfo) {
		addLoginHistory(userLoginInfo, DictConstants.LOGINHISTORY_FAIL);
	}

	/**
	 * 添加成功的登录历史记录
	 */
	@Override
	public void addSuccessLoginHistory(UserLoginInfo userLoginInfo) {
		addLoginHistory(userLoginInfo, DictConstants.LOGINHISTORY_SUCCESS);
	}

	/**
	 * 是否验证成功
	 */
	@Override
	public Account authenticateLogin(String userName, String userPwd) {
		return  accountDao.findUserBynameWithPwd(userName, userPwd,DictConstants.ACCOUNT_YES);
	}
	

	@Override
	public void updatePwd(String newPwd) {
		Long loginId = requestUtil.getAccountId();
		//通過id更新密碼
		accountDao.updatePwdById(loginId,newPwd);
	}

	@Override
	public GraphicCode getCode(String uuid, String validateCode) {
		return loginMapper.getCode(uuid,validateCode);
	}

	@Override
	public void saveCode(String uuid, String createText) {
		loginMapper.saveCode(uuid,createText);		
	}

	@Override
	public void updateSessionInfo(String id, Long loginId, Date validity) {
		loginMapper.updateSessionInfo(id,loginId,validity);		
	}

	@Override
	public void updateloginTime(Long loginId) {
		loginMapper.updateloginTime(loginId);
	}

	@Override
	public void insertSession(long id) {
		loginMapper.insertSession(id);		
	}

	@Override
	public Session findBySessionId(String sessionId) {
	 return	loginMapper.findBySessionId(sessionId);
	}

	@Override
	public void updateAppSessionInfo(String id, Long loginId) {
		Map<String, String> pa = new HashMap<String, String>();
		pa.put("sessionid", id);
		pa.put("userid", loginId+"");
		
		
		loginMapper.updateAppSessionInfo(pa);	
	}

	@Override
	public Account authenticateAppLogin(String userName, String userPwd) {
		
		return  accountDao.authenticateAppLogin(userName, userPwd,DictConstants.ACCOUNT_YES);
	}

	@Override
	public AppLoginRespInfo getUserInfo(Long loginId) 
	{
		
		AppLoginRespInfo info = loginMapper.getUserInfo(loginId);
		
		Map<String, String> pa = new HashMap<String, String>();
		pa.put("add_id", info.getId());
		pa.put("type", "1");
		String duty = appMapper.getOrganizationByAddressBookId(pa);
		pa.put("type", "2");
		String organs = appMapper.getOrganizationByAddressBookId(pa);
		info.setDuty(duty);	
		info.setOrganizations(organs);
		
		return info;
	}

	@Override
	public void delSessionInfo(String sessionId) {
		loginMapper.delSessionInfo(sessionId);
	}
	
}
